<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中国移动AI客服</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #e60012;
            text-align: center;
        }
        #chat-history {
            height: 400px;
            overflow-y: auto;
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 5px;
            background-color: #fafafa;
        }
        .message {
            margin-bottom: 15px;
            padding: 10px;
            border-radius: 5px;
        }
        .user-message {
            background-color: #d1e7ff;
            text-align: right;
        }
        .ai-message {
            background-color: #e2e3e5;
        }
        #input-form {
            display: flex;
            gap: 10px;
        }
        #user-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            padding: 10px 20px;
            background-color: #e60012;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #c4000f;
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        .session-info {
            margin-bottom: 15px;
            padding: 10px;
            background-color: #e7f3ff;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>中国移动AI客服助手</h1>
        
        <div class="session-info">
            <label for="session-id">会话ID:</label>
            <input type="text" id="session-id" value="session-1" readonly>
            <button onclick="generateNewSession()">新会话</button>
        </div>
        
        <div id="chat-history"></div>
        
        <form id="input-form">
            <input type="text" id="user-input" placeholder="请输入您的问题..." required>
            <button type="submit">发送</button>
        </form>
    </div>

    <script>
        // 生成新的会话ID
        function generateNewSession() {
            document.getElementById('session-id').value = 'session-' + Date.now();
            document.getElementById('chat-history').innerHTML = '';
        }

        // 添加消息到聊天历史
        function addMessageToHistory(message, isUser) {
            const chatHistory = document.getElementById('chat-history');
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${isUser ? 'user-message' : 'ai-message'}`;
            messageDiv.textContent = message;
            chatHistory.appendChild(messageDiv);
            chatHistory.scrollTop = chatHistory.scrollHeight;
        }

        // 调用AI服务
        async function callAIService(sessionId, userMessage) {
            try {
                const response = await fetch(`/ai/service?sessionId=${encodeURIComponent(sessionId)}`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(userMessage)
                });

                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }

                const data = await response.text();
                return data;
            } catch (error) {
                console.error('Error calling AI service:', error);
                return "抱歉，AI服务暂时不可用，请稍后再试。";
            }
        }

        // 处理表单提交
        document.getElementById('input-form').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const userInput = document.getElementById('user-input');
            const sessionIdInput = document.getElementById('session-id');
            
            const userMessage = userInput.value.trim();
            const sessionId = sessionIdInput.value;
            
            if (!userMessage) return;
            
            // 禁用输入和按钮
            userInput.disabled = true;
            document.querySelector('#input-form button').disabled = true;
            
            // 显示用户消息
            addMessageToHistory("我: " + userMessage, true);
            userInput.value = '';
            
            // 调用AI服务并显示回复
            const aiResponse = await callAIService(sessionId, userMessage);
            addMessageToHistory("AI客服: " + aiResponse, false);
            
            // 启用输入和按钮
            userInput.disabled = false;
            document.querySelector('#input-form button').disabled = false;
            userInput.focus();
        });

        // 初始化
        document.addEventListener('DOMContentLoaded', function() {
            document.getElementById('user-input').focus();
        });
    </script>
</body>
</html>